﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Adp.Pleiade.Fwk.Entity;
using Adp.Pleiade.Fwk.Referentiels.Data;
using Adp.Pleiade.Fwk.Service.WCF;
using Adp.Pleiade.Fwk.Referentiels.EntitesReferentielMultiple;
using Adp.Pleiade.Fwk.Calcul.UI;
using System.Reflection;

namespace OutillageTests
{
    public static class Tools
    {
        public static Type GetNormalizedType(Type unType)
        {
            if (unType.HasElementType) return unType.GetElementType();
            else return unType;
        }

        /// <summary>
        /// Reconnaissance des types supportés
        /// </summary>
        /// <param name="unType"></param>
        /// <returns>retourne vrai le type passé en paramètre est supporté, faux dans le cas contraire</returns>
        public static bool CheckForUnrecognizedType(Type unType)
        {
            return !(typeof(EntityBase).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(List<EntityBase>).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(List<VueDonneesReference>).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(VueDonneesReference).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(AncestorService).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(IAncestorService).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(RM_TableBase).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(List<RM_TableBase>).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        typeof(FaceAvantBase).IsAssignableFrom(Tools.GetNormalizedType(unType)) ||
                        Tools.GetNormalizedType(unType).ToString().Contains("System.Predicate"));
        }

        public static bool isStructureType(Type unType)
        {
            return unType.IsValueType &&
                   !unType.IsEnum &&
                   !unType.IsPrimitive &&
                    unType != typeof(decimal) &&
                    unType != typeof(int) &&
                    unType != typeof(double) &&
                    unType != typeof(float) &&
                    unType != typeof(decimal) &&
                    unType != typeof(DateTime);
        }

        public static string DonnerFormatDeclarationDictionary(Type unType, bool forXML)
        {
            string chain;
            string ouverture = "<";
            string fermeture = ">";
            if (forXML)
            {
                ouverture = "[";
                fermeture = "]";
            }
            chain = "Dictionary" + ouverture;
            for (int i = 0; i < unType.GetGenericArguments().Count(); i++)
            {
                if (i > 0) chain += ", ";
                chain += Tools.GetNormalizedType(unType.GetGenericArguments()[i]);
            }
            chain += fermeture;
            return chain;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="unType"></param>
        /// <param name="forXML">Indique si la déclaration doit être utilisée en XML ("[" utilisé)</param>
        /// <returns></returns>
        public static string DonnerFormatDeclarationList(Type unType, bool forXML)
        {
            string ouverture = "<";
            string fermeture = ">";
            if (forXML)
            {
                ouverture = "[";
                fermeture = "]";
            }
            return ("List" + ouverture + Tools.GetNormalizedType(unType.GetGenericArguments()[0]) + fermeture);
        }

        public static string DonnerChaineDeclarationMethode(MethodInfo method)
        {
            string result = "";
            
            // Accessibilité méthode
            if (method.IsPublic) result = "public ";
            else if (method.IsPrivate) result = "private ";
            else result = "protected ";

            // Void ?
            if (GetNormalizedType(method.ReturnType) == typeof(void)) result += "void ";
            else result += GetNormalizedType(method.ReturnType).ToString() + " ";

            // Nom de la méthode
            result += method.Name + "(";

            // Paramètres
            for (int i = 0; i < method.GetParameters().Length; i++)
            {
                if (i > 0) result += ", ";
                result += GetNormalizedType(method.GetParameters()[i].ParameterType) + " " +
                          method.GetParameters()[i].Name;   
            }

            result += ")";

            return result;
        }
    }
}
